from django.shortcuts import render
from django.views import View
from contents.utils import get_category
from contents.models import ContentCategory ,Content


class IndexView(View):
    def get(self, request):


        categories = get_category()
        # 首页商品推荐广告数据
        # 获取所有的推荐商品广告类别
        content_categories = ContentCategory.objects.all()
        contents = {}
        for content_category in content_categories:
            contents[content_category.key] = Content.objects.filter(
                category_id=content_category.id,
                status=True
            ).all().order_by('sequence')

        context = {'categories': categories, 'contents': contents}







        if not request.user.is_authenticated:
            response = render(request , 'index.html',context)
            response.delete_cookie('username')
        else:
            username = request.user.username
            print(username)
            response = render(request , 'index.html',context)
            response.set_cookie('username', username, 3600)


        return response
